package com.vito.oa.preview.util;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.PropertyResourceBundle;

import javax.servlet.http.HttpServletResponse;

import com.vito.base.util.StringUtil;

public class PreviewUtil {
	/**如果是office文档，需要做pdf转换*/
	public static List<String> officeFileList;
	/**如果是是可以直接打开的文档，不需要做pdf转换*/
	public static List<String> fileDirectShowList;
	
	public static List<String> imageList;
	
	public static Map<String,String> fileDirectShowMapForHeader;
	
	/**文件转换服务器地址*/
	public final static String FILE_CONVERT_HOST = StringUtil.getProperties("conf/preview").getString("preview.outpath");
	public final static String FILE_CONVERT_PATH = StringUtil.getProperties("conf/preview").getString("online.convert.host");
	static{
		officeFileList=new ArrayList<String>();
		officeFileList.add("doc");
		officeFileList.add("docx");
		officeFileList.add("xls");
		officeFileList.add("xlsx");
		officeFileList.add("ppt");
		officeFileList.add("pptx");
		officeFileList.add("txt");
		
		fileDirectShowList=new ArrayList<String>();
		fileDirectShowList.add("pdf");
		fileDirectShowList.add("bmp");
		fileDirectShowList.add("gif");
		fileDirectShowList.add("ief");
		fileDirectShowList.add("jpeg");
		fileDirectShowList.add("jpg");
		fileDirectShowList.add("png");
		fileDirectShowList.add("tiff");
		fileDirectShowList.add("tif");
		
		imageList= new ArrayList<>();
		imageList.add("gif");
		imageList.add("ief");
		imageList.add("jpeg");
		imageList.add("jpg");
		imageList.add("png");
		imageList.add("tiff");
		imageList.add("tif");
		
		fileDirectShowMapForHeader=new HashMap<String,String>();
		fileDirectShowMapForHeader.put("pdf", "application/pdf");
		fileDirectShowMapForHeader.put("bmp", "image/bmp");
		fileDirectShowMapForHeader.put("gif", "image/gif");
		fileDirectShowMapForHeader.put("ief", "image/ief");
		fileDirectShowMapForHeader.put("jpeg", "image/jpeg");
		fileDirectShowMapForHeader.put("jpg", "image/jpeg");
		fileDirectShowMapForHeader.put("png", "image/png");
		fileDirectShowMapForHeader.put("tiff", "image/tiff");
		fileDirectShowMapForHeader.put("tif", "image/tif");
	}
	
	
	public static boolean isOfficeFile(String fileName){
		return officeFileList.contains(fileName.substring(fileName.lastIndexOf(".")+1));
	}
	public static boolean isFileCanShow(String fileName){
		return fileDirectShowList.contains(fileName.substring(fileName.lastIndexOf(".")+1));
	}
	public static boolean isNotOfficeFile(String type){
		return fileDirectShowList.contains(type.toLowerCase());
	}
	
	public static void fileDirectShow(String filename,InputStream fileIs, HttpServletResponse response) throws Exception {
		String extName = filename.substring(filename.lastIndexOf(".")+1).toLowerCase();
		response.setHeader("Content-Disposition", "filename=" + filename);
		response.setHeader("Content-type", fileDirectShowMapForHeader.get(extName));
		
		byte data[] = new byte[1024];
		OutputStream outStream = response.getOutputStream(); // 得到向客户端输出二进制数据的对象
		int readCount = 0;
        readCount = fileIs.read(data);
        while (readCount != -1) {
        	outStream.write(data, 0, readCount);
            readCount = fileIs.read(data);
        }
		outStream.flush();
		outStream.close();
		fileIs.close();
	}
}
